package com.xcfw.dorm.controller.login;

import com.xcfw.dorm.controller.BaseController;
import com.xcfw.dorm.entity.NumEnum;
import com.xcfw.dorm.entity.ReturnJson;
import com.xcfw.dorm.exception.ServiceException;
import com.xcfw.dorm.exception.ValidationException;
import com.xcfw.dorm.seviceimpl.LoginServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @program: Dorm
 * @description: 登录控制层
 * @author: Bruse Queen
 * @create: 2018-04-09 16:07
 **/
@RestController
@Slf4j
@CrossOrigin
public class LoginController extends BaseController {

    @Autowired
    private LoginServiceImpl loginService;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ReturnJson login(@RequestBody final Account account) {
        try {
            result = loginService.login(account.username, account.password);
        } catch (ValidationException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (ServiceException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (Exception e) {
            log.error("AssistantController---addAssistant--error", e);
            result = new ReturnJson(NumEnum.FAIL.getIndex(), "数据异常,请稍后再试！");
        }
        return result;
    }

    private boolean isValidPassword(String username, String password) {
        return "admin".equals(username)
                && "admin".equals(password);
    }

//    @Bean
//    public FilterRegistrationBean jwtFilter() {
//        final FilterRegistrationBean registrationBean = new FilterRegistrationBean();
//        JwtAuthenticationFilter filter = new JwtAuthenticationFilter();
//        registrationBean.setFilter(filter);
//        return registrationBean;
//    }

    public static class Account {
        public String username;
        public String password;
    }

}
